<template>
	<div class="test">
		<div class="my-input-number" style="width:180px;height:40px;display:flex;border: 1px solid #999;padding-left: 0;">
			<input type="button" value="-" style="width:40px;font-size:25px;padding-left: 0;" @click="reduce">
			<input type="text" v-model="count" style="outline:none;width:100px;font-size:25px;text-align:center;padding-right:0;padding-left: 0;" @input="change" @blur="checkCount">
			<input type="button" value="+" style="width:40px;font-size:25px;padding-right: 0;" @click="add">
		</div>
	</div>

</template>

<script>
	export default {
		props: {

			// 最小值默认0
			min: {
				type: Number,
				default: 1
			},
			// 最大值默认10
			max: {
				type: Number,
				default: 100
			},
			obj_: {
				type: Object,
				default: {}
			},
			key_: {
				type: String,
				default: null
			}
		},
		data() {
			return {
				validCount: 1,
				count: 1
			}
		},
		mounted() {
			this.validCount = this.obj_[this.key_];
			this.count = this.obj_[this.key_];
		},
		computed: {　　

		},
		watch: {
			value(val, oldVal) {
				// 				console.log(val,oldVal)
			}
		},
		methods: {
			// 增加
			add() {
				this.count++;
				this.change(true);
			},
			// 减少
			reduce() {
				this.count--;
				this.change(true);
			},
			// 个数改变 做数据类型转换
			change(isCheck=false) {
				this.count = parseInt(this.count); // 解析字符串返回整数
				console.log(isNaN(this.count))
				if(isNaN(this.count)) {
					this.count = 1;
				}
				if(this.count > this.max) {
					this.count = this.max
				} else if(this.count < this.min) {
					this.count = this.min;
				} else if(this.count > 999999) {
					this.count = 999999;
				}
//				this.obj_[this.key_] = this.count;
				if(isCheck===true){
					 this.checkCount();
				}
				
//				this.$emit('change', this.obj_, this.count, true)
			},
			async checkCount() {
				if(this.validCount == this.count){
					return;
				}
				let params = {};
				params.id = this.obj_.id;
				params.qty  =this.count;
				let res = await this.httptool.post(this.apis.shopCareEditQty, params,aa=>{
					this.count = this.validCount;
				});
				this.obj_[this.key_] = this.count;
				this.validCount = this.count;
				this.$emit('change')
			}
		}
	}
</script>
<style lang="less">
	.test {
		input {
			border: 1px solid #999;
			padding-left: 10px;
		}
	}
</style>